#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re
import sys
import os
import time
import threading
import datetime
import random


def runMysql():
    #os.system("sudo service mysql restart > /dev/null 2>&1")
    starttime = datetime.datetime.now()
    os.system("./exe > /dev/null 2>&1")
    os.system("./exe > /dev/null 2>&1")

#    time.sleep(10)
    endtime = datetime.datetime.now()
    print "Mysql:" + str((endtime - starttime).total_seconds())
    #os.system("sudo service mysql restart > /dev/null 2>&1")

def runRedis():
    #os.system("redis-cli FLUSHALL")
    #os.system("sudo service redis-server restart > /dev/null 2>&1")
    #time.sleep(2)
    starttime = datetime.datetime.now()
    for num in range(1,16000):
        os.system("redis-cli -h 127.0.0.1 -p 6379 get key%d > /dev/null 2>&1" % (random.randint(1,10000)))
#   time.sleep(10)
    endtime = datetime.datetime.now()
    print "Redis:" + str((endtime - starttime).total_seconds())
    #os.system("sudo service redis-server restart > /dev/null 2>&1")

def runHttpd():
    os.system("sudo service apache2 restart > /dev/null 2>&1")
    starttime = datetime.datetime.now()
    os.system("ab -n 400000 -c 500 http://127.0.0.1/index.html > /dev/null 2>&1")
    endtime = datetime.datetime.now()
    print "Httpd:" + str((endtime - starttime).total_seconds())
    os.system("sudo service apache2 restart > /dev/null 2>&1")
    
if __name__ == '__main__':

    threadMysql = threading.Thread(target=runMysql, args=())
    threadRedis = threading.Thread(target=runRedis, args=())
    #threadHttpd = threading.Thread(target=runHttpd, args=())
    threadRedis.start()
    threadMysql.start()
    #threadHttpd.start()
    
    threadMysql.join()
    threadRedis.join()
    #threadHttpd.join()
